package springboot.study.project.service.impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot.study.project.dto.ClazzDto;
import springboot.study.project.dto.MajorDto;
import springboot.study.project.mapper.ClazzMapper;
import springboot.study.project.mapper.DepartmentMapper;
import springboot.study.project.mapper.MajorMapper;
import springboot.study.project.model.*;
import springboot.study.project.service.CommonService;
import springboot.study.project.util.common.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CommonServiceImpl
 * @Description: 通用借口实现类
 * @Author: liu-hao
 * @Date: 2021-04-11 15:42
 * @Version: 1.0
 **/
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    MajorMapper majorMapper;
    @Autowired
    ClazzMapper clazzMapper;

    @Override
    public List<Department> getAllDept() {
        List<Department> departments = departmentMapper.selectByExample(new DepartmentExample());
        return departments;
    }

    @Override
    public boolean addDept(Department department) {
        DepartmentExample example = new DepartmentExample();
        example.createCriteria().andDeptNameEqualTo(department.getDeptId());
        int count = departmentMapper.countByExample(example);
        if (count > 0) {
            return false;
        }
        int insertCount = departmentMapper.insert(department);
        return insertCount > 0 ? true : false;
    }

    @Override
    public boolean removeDept(String deptId) {
        if (StringUtil.isBlank(deptId)) {
            return false;
        }
        int deleteCount = departmentMapper.deleteByPrimaryKey(deptId);
        return deleteCount > 0 ? true : false;
    }

    @Override
    public boolean modifyDept(Department department) {
        if (StringUtil.isBlank(department.getDeptId())) {
            return false;
        }
        int updateCount = departmentMapper.updateByPrimaryKeySelective(department);
        return updateCount > 0 ? true : false;
    }

    @Override
    public Map<String, Object> findDeptInfo(String deptName, int offset, int pageSize) {
        DepartmentExample example = new DepartmentExample();
        if (StringUtil.isNotBlank(deptName))
            example.createCriteria().andDeptNameLike("%" + deptName + "%");

        // 查询符合条件宿舍数量
        int totalCount = departmentMapper.countByExample(example);
        // 查询符合条件的宿舍信息
        List<Department> departments = departmentMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));

        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", departments);
        return resultMap;
    }

    @Override
    public List<Major> getMajorByDeptId(String deptId) {
        MajorExample majorExample = new MajorExample();
        if (StringUtil.isNotBlank(deptId)) {
            majorExample.createCriteria().andDeptIdEqualTo(deptId);
        }
        List<Major> majors = majorMapper.selectByExample(majorExample);
        return majors;
    }

    @Override
    public boolean addMajor(Major major) {
        MajorExample example = new MajorExample();
        example.createCriteria().andMajorNameEqualTo(major.getMajorId());
        int count = majorMapper.countByExample(example);
        if (count > 0) {
            return false;
        }
        int insertCount = majorMapper.insert(major);
        return insertCount > 0 ? true : false;
    }

    @Override
    public boolean removeMajor(String majorId) {
        if (StringUtil.isBlank(majorId)) {
            return false;
        }
        int deleteCount = majorMapper.deleteByPrimaryKey(majorId);
        return deleteCount > 0 ? true : false;
    }

    @Override
    public boolean modifyMajor(Major major) {
        if (StringUtil.isBlank(major.getMajorId())) {
            return false;
        }
        int updateCount = majorMapper.updateByPrimaryKeySelective(major);
        return updateCount > 0 ? true : false;
    }

    @Override
    public Map<String, Object> findMajorInfo(String majorName, int offset, int pageSize) {
        MajorExample example = new MajorExample();
        if (StringUtil.isNotBlank(majorName))
            example.createCriteria().andMajorNameLike("%" + majorName + "%");

        // 查询符合条件宿舍数量
        int totalCount = majorMapper.countByExample(example);
        // 查询符合条件的宿舍信息
        List<Major> majors = majorMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));

        List<MajorDto> majorDtos = new ArrayList<>();
        for (Major major : majors) {
            MajorDto majorDto = new MajorDto();
            if (StringUtil.isNotBlank(major.getDeptId())) {
                Department department = departmentMapper.selectByPrimaryKey(major.getDeptId());
                if (department != null) {
                    majorDto.setDeptName(department.getDeptName());
                }
            }
            BeanUtils.copyProperties(major, majorDto);

            majorDtos.add(majorDto);
        }
        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", majorDtos);
        return resultMap;
    }

    @Override
    public List<Clazz> getClazzByMajorId(String majorId) {
        ClazzExample ClazzExample = new ClazzExample();
        if (StringUtil.isNotBlank(majorId)) {
            ClazzExample.createCriteria().andMajorIdEqualTo(majorId);
        }
        List<Clazz> classes = clazzMapper.selectByExample(ClazzExample);
        return classes;
    }

    @Override
    public boolean addClazz(Clazz clazz) {
        ClazzExample example = new ClazzExample();
        example.createCriteria().andClazzIdEqualTo(clazz.getClazzId());
        int count = clazzMapper.countByExample(example);
        if (count > 0) {
            return false;
        }
        int insertCount = clazzMapper.insert(clazz);
        return insertCount > 0 ? true : false;
    }

    @Override
    public boolean removeClazz(String clazzId) {
        if (StringUtil.isBlank(clazzId)) {
            return false;
        }
        int deleteCount = clazzMapper.deleteByPrimaryKey(clazzId);
        return deleteCount > 0 ? true : false;
    }

    @Override
    public boolean modifyClazz(Clazz clazz) {
        if (StringUtil.isBlank(clazz.getClazzId())) {
            return false;
        }
        int updateCount = clazzMapper.updateByPrimaryKeySelective(clazz);
        return updateCount > 0 ? true : false;
    }

    @Override
    public Map<String, Object> findClazzInfo(String clazzName, int offset, int pageSize) {
        ClazzExample example = new ClazzExample();
        if (StringUtil.isNotBlank(clazzName))
            example.createCriteria().andClazzNameLike("%" + clazzName + "%");

        // 查询符合条件宿舍数量
        int totalCount = clazzMapper.countByExample(example);
        // 查询符合条件的宿舍信息
        List<Clazz> clazzes = clazzMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));

        List<ClazzDto> clazzDtos = new ArrayList<>();
        for (Clazz clazz : clazzes) {
            ClazzDto clazzDto = new ClazzDto();
            if (StringUtil.isNotBlank(clazz.getMajorId())) {
                Major major = majorMapper.selectByPrimaryKey(clazz.getMajorId());
                if (major != null) {
                    clazzDto.setMajorName(major.getMajorName());
                    Department department = departmentMapper.selectByPrimaryKey(major.getDeptId());
                    if (department != null) {
                        clazzDto.setDeptId(department.getDeptId());
                        clazzDto.setDeptName(department.getDeptName());
                    }
                }
            }
            BeanUtils.copyProperties(clazz, clazzDto);
            clazzDtos.add(clazzDto);
        }
        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", clazzDtos);
        return resultMap;
    }
}
